﻿import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @version 1.1 05 Mar 2014
 * @author Team Four
 * @review Leader
Dũng là con chó
cái lồng
 */

public class clsCalculatorSimple extends JFrame implements ActionListener, KeyListener{

	private JTextField txtInput1, txtInput2, txtResult;
	private JComboBox cboOperation = new JComboBox<>();
	private JButton btnSolve, btnReset, btnExit;
	private JLabel lblInput1, lblInput2, lblOperation, lblResult;
	private JPanel pnlTop, pnlButton;	
	private double num1, num2, result;
	private String snum1,snum2;
	
	public clsCalculatorSimple() {							//Hàm thiết kế giao diện
		super("Simple Calculator");
		pnlTop = new JPanel();
		pnlButton = new JPanel();
		pnlTop.setLayout(new GridLayout(4, 2, 5, 5));
		pnlButton.setLayout(new GridLayout(1, 3, 2, 2));

		lblInput1 = new JLabel("Input the first operand:");
		lblInput2 = new JLabel("Input the second operand:");
		lblOperation = new JLabel("Choose the operator:");
		lblResult = new JLabel("Result:");

		btnSolve = new JButton("Solve");
		btnReset = new JButton("Reset");
		btnExit = new JButton("Exit");

		txtInput1 = new JTextField(20);
		txtInput1.setBackground(Color.WHITE);
		txtInput1.setText("");
		txtInput1.addKeyListener(this);
		txtInput1.setFocusTraversalKeysEnabled(false);

		txtInput2 = new JTextField(20);
		txtInput2.setBackground(Color.WHITE);
		txtInput2.setText("");
		txtInput2.addKeyListener(this);
        txtInput2.setFocusTraversalKeysEnabled(false);

		txtResult = new JTextField(20);
		txtResult.setEditable(false);
		txtResult.setBackground(Color.YELLOW);
		txtResult.setText("");
		txtResult.addKeyListener(this);
        txtResult.setFocusTraversalKeysEnabled(false);
	
		cboOperation.setBounds(200, 200, 180, 40);
		cboOperation.addItem("+");
		cboOperation.addItem("-");
		cboOperation.addItem("*");
		cboOperation.addItem("/");

		pnlButton.add(btnSolve);
		pnlButton.add(btnReset);
		pnlButton.add(btnExit);

		pnlTop.add(lblInput1);
		pnlTop.add(txtInput1);
		pnlTop.add(lblInput2);
		pnlTop.add(txtInput2);
		pnlTop.add(lblOperation);
		pnlTop.add(cboOperation);
		pnlTop.add(lblResult);
		pnlTop.add(txtResult);

		btnSolve.addActionListener(this);
		btnReset.addActionListener(this);
		btnExit.addActionListener(this);
		
		txtInput1.addActionListener(this);
		txtInput2.addActionListener(this);
		
		setLayout(new BorderLayout());
		add(pnlTop, BorderLayout.NORTH);
		add(pnlButton, BorderLayout.SOUTH);
		setResizable(false);
		setSize(320, 180);
		setVisible(true);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public static void main(String[] args) {
		new clsCalculatorSimple();
	}

	public boolean check(String z) {						//Kiểm tra nhập vào chữ hay số
		try {
			Double.parseDouble(z);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public void solve() {									//Hàm xử lý
		int kt=0;
		num1 = Double.parseDouble(snum1);
		num2 = Double.parseDouble(snum2);
		switch (cboOperation.getSelectedIndex()) {
			case 0:
				result = num1 + num2;
				break;
			case 1:
				result = num1 - num2;
				break;
			case 2:
				result = num1 * num2;
				break;
			case 3:
				if(num2==0)
					kt=1;
				else
					result = num1 / num2;
				break;
			default: break;
		}

		if (kt == 0)
			if (result == (int) result)
				txtResult.setText(String.valueOf((int) result));
			else
				txtResult.setText(String.valueOf(result));
		else
			txtResult.setText(String.valueOf("Loi chia cho 0"));
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnReset) {
			txtInput1.setText("");
			txtInput2.setText("");
			txtResult.setText("");
			txtInput1.requestFocus();
		} else if (e.getSource() == btnSolve) {
			snum1 = txtInput1.getText();
			snum2 = txtInput2.getText();
			if (snum1.length() == 0 || check(snum1) == false) {
				JOptionPane.showMessageDialog(null,	"Bạn đã nhập sai, mời nhập lại !");
				txtInput1.requestFocus();
				txtInput1.setText("");
			} else if (snum2.length() == 0 || check(snum2) == false) {
				JOptionPane.showMessageDialog(null, "Bạn đã nhập sai, mời nhập lại !");
				txtInput2.requestFocus();
				txtInput2.setText("");
			} else {
				solve();
			}
		}
		if (e.getSource() == btnExit) {
			System.exit(0);
		}
	}

	public void keyPressed(KeyEvent e) {
		if (e.getSource() == txtInput1) {
			if (e.getKeyCode() == e.VK_ENTER || e.getKeyCode() == e.VK_DOWN || e.getKeyCode() == e.VK_TAB) {
				String h = txtInput1.getText();
				if (h.length() == 0 || check(h) == false) {
					JOptionPane.showMessageDialog(null, "Bạn đã nhập sai, mời nhập lại !");
					txtInput1.requestFocus();
					txtInput1.setText("");
				} else
					txtInput2.requestFocus();
			}
		}

		if (e.getSource() == txtInput2) {
			if (e.getKeyCode() == e.VK_ENTER || e.getKeyCode() == e.VK_DOWN	|| e.getKeyCode() == e.VK_TAB || e.getKeyCode() == e.VK_UP) {
				String h = txtInput2.getText();
				if (h.length() == 0 || check(h) == false) {
					JOptionPane.showMessageDialog(null,	"Bạn đã nhập sai, mời nhập lại !");
					txtInput2.requestFocus();
					txtInput2.setText("");
				} else if (e.getKeyCode() == e.VK_UP)
					txtInput1.requestFocus();
				else
					cboOperation.requestFocus();
			}
		}
	}

	public void keyReleased(KeyEvent e) {}
	public void keyTyped(KeyEvent e) {}

}